﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using AAttributeServiceLibrary1;
using GroupsServiceLibrary;
using AttributesGroupsServiceLibrary;

namespace NascarEventsWPF
{
    
    public partial class Page2_UsersandGroups : Window
    {
        //creates objects of service classes
        AttributeService1 atc = new AttributeService1();
        AttributeGroupsService1 agc = new AttributeGroupsService1();
        GroupsService1 gs = new GroupsService1();

        public Page2_UsersandGroups()
        {
            InitializeComponent();
        }

       
//--------------------------Page Load -------------------------

        private void Window_Loaded_1(object sender, RoutedEventArgs e)
        {
            //fills datatables for attributes and groups for display
            DataTable attribTable = atc.FillDataTable("tbAttributes");
            DataTable groupTable = gs.FillDataTable("tbGroups");

            //fills datagrid attributes            
            attributes_datagrid.ItemsSource = attribTable.DefaultView;

            //fills datagrid groups
            groups_datagrid.ItemsSource = groupTable.DefaultView;
        }
      

//----------------------------------Attribute Methods------------------------       

       //attribute search by the name in search box
        private void search_Attribute_button_Click(object sender, RoutedEventArgs e)
        {
            
            Attribute_Results_txt.Text = "";
            string selectBy = "AttributeName = '" + attribute_search_box.Text + "'";
            DataTable dt = atc.FillDataTable("tbAttributes");
            DataRow[] row_result = dt.Select(selectBy);

            if (row_result.Length > 0)
            {
                foreach (DataRow rows in row_result) //loop through each row
                {
                    attributeID_txb.Text = Convert.ToString(rows["AttributeID"]);
                    attributeName_txt.Text = Convert.ToString(rows["AttributeName"]);
                    attributeType_txt.Text = Convert.ToString(rows["AttributeType"]);
                    attributeDescription_txt.Text = Convert.ToString(rows["AttributeDescription"]);
                    string attributeDisabled = Convert.ToString(rows["AttributeDisabled"]);

                    if (attributeDisabled == "N")
                    {
                        disableN_rb.IsChecked = true;
                    }
                }

            }
            else
            {
                Attribute_Results_txt.Text = "Attribute not found";
            }
        }


        // add attribute method
        private void AddAttribute_btn_Click(object sender, RoutedEventArgs e)
        {
            // this is how you get the information from the text box into a variable
            //they are adding an attribute name, type,description and if it is disabled
            string an = attributeName_txt.Text;
            string at = attributeType_txt.Text;
            string adp = attributeDescription_txt.Text;
            string ad = "";
            // this is the radio button code  
            // if N is check than ad is set as N, otherwise it's Y
            if (disableN_rb.IsChecked == true)
            { ad = "N"; }
            else
            { ad = "Y"; }

            // this is where the method InsertRowinAttributeTable(an, at, adp, ad)  is called
            //and send it the 
            Attribute_Results_txt.Text = atc.InsertRowinAttributeTable(an, at, ad, adp);
        }

        //update method
        private void updateAttribute_btn_Click(object sender, RoutedEventArgs e)
        {
            //get information from text boxes
            string aid = attributeID_txb.Text;
            string an = attributeName_txt.Text;
            string at = attributeType_txt.Text;
            string adp = attributeDescription_txt.Text;
            string ad = "";
            //radio button
            if (disableN_rb.IsChecked == true)
            { ad = "N"; }
            else
            { ad = "Y"; }

            Attribute_Results_txt.Text = "";
            //calls editrow method  to update database
            Attribute_Results_txt.Text = atc.EditRowinAttributeTable(aid, an, at, adp, ad);
        }

        //refresh after changes to database
        private void btnRefreshAttrib_Click(object sender, RoutedEventArgs e)
        {
            DataTable attribTable = atc.FillDataTable("tbAttributes");
            attributes_datagrid.ItemsSource = attribTable.DefaultView;
        }
        //clears textboxes
        private void clearAttribute_Click(object sender, RoutedEventArgs e)
        {
            attribute_search_box.Clear();
            attributeID_txb.Text = String.Empty;
            attributeName_txt.Clear();
            attributeType_txt.Clear();
            attributeDescription_txt.Clear();
            Attribute_Results_txt.Clear();
        }

//-----------------------Group methods --------------------------------------

 // gets new information from user and calls insert row to create new group
        private void groupAdd_btn_Click(object sender, RoutedEventArgs e)
        {
            string gn = groupName_txt.Text;
            string gp = groupPriority_txt.Text;
            string gdp = groupDescription_txt.Text;

            GroupResults_txt.Text = gs.InsertRowinGroupTable(gn, gp, gdp);
        }

 //gets group from text box and searches for it in group table of database
        private void search_button_Group_Click(object sender, RoutedEventArgs e)
        {
            
            GroupResults_txt.Text = "";
            string selectBy = "GroupName = '" + group_search_box.Text + "'";
            DataTable dt = gs.FillDataTable("tbGroups");
            DataRow[] row_result = dt.Select(selectBy);

            if (row_result.Length > 0)
            {
                foreach (DataRow rows in row_result) //loop through each row
                {
                    //to put each item into textboxes for update/edit
                    groupID_txb.Text = Convert.ToString(rows["GroupID"]);
                    groupName_txt.Text = Convert.ToString(rows["GroupName"]);
                    groupPriority_txt.Text = Convert.ToString(rows["GroupPriority"]);
                    groupDescription_txt.Text = Convert.ToString(rows["GroupDescription"]);

                    //if you just want to return one long string with spaces between items:
                    foreach (var item in rows.ItemArray)
                    { GroupResults_txt.Text = GroupResults_txt.Text + item + " "; }
                }
            }
            else
            {
                GroupResults_txt.Text = "Group not found";
            }

        }
 // gets changes from user and calls update group method
        private void groupEdit_btn_Click(object sender, RoutedEventArgs e)
        {
            string gid = groupID_txb.Text;
            string gn = groupName_txt.Text;
            string gp = groupPriority_txt.Text;
            string gd = groupDescription_txt.Text;

            GroupResults_txt.Text = "";
            GroupResults_txt.Text = gs.UpdateGroup(gid, gn, gp, gd);
        }

//clears textboxes
        private void cleargroup_btn_Click(object sender, RoutedEventArgs e)
        {
            GroupResults_txt.Clear();
            groupID_txb.Text = String.Empty;
            groupName_txt.Clear();
            groupPriority_txt.Clear();
            groupDescription_txt.Clear();
        }
//refresh after changes to database
        private void btnRefreshGrp_Click(object sender, RoutedEventArgs e)
        {
            DataTable groupTable = gs.FillDataTable("tbGroups");
            groups_datagrid.ItemsSource = groupTable.DefaultView;
        }


    }//end class



}//end namespace
